param(
  [string]$Env = "prod",
  [string]$Tag = "nexusgo-web-1.0.0",
  [string]$BackendHost = "111.231.17.243:9000"
)
Write-Host "=== Using Docker to build frontend image (env: $Env, tag: $Tag) ==="

# 注入 Compose 变量（镜像标签与后端地址）
$env:IMAGE_TAG = $Tag
$env:BACKEND_HOST = $BackendHost

Write-Host "`n=== Checking Docker engine status ==="
docker --version
if ($LASTEXITCODE -ne 0) {
    Write-Host "=== Docker 未安装或未在 PATH 中 ===" -ForegroundColor Red
    exit 1
}

docker info
if ($LASTEXITCODE -ne 0) {
    Write-Host "=== Docker Desktop 未运行或引擎不可用，请先启动 Docker Desktop（WSL2/Windows engine）后重试 ===" -ForegroundColor Red
    exit 1
}

Write-Host "`n=== Starting frontend Docker image build ==="
docker compose -f docker-compose.yml build
if ($LASTEXITCODE -ne 0) {
    Write-Host "=== Frontend image build failed ===" -ForegroundColor Red
    exit 1
}


Write-Host "`n=== Starting frontend image push to repository ==="
docker push "crpi-gpd8vhj1nlvns47d.cn-hangzhou.personal.cr.aliyuncs.com/xiao-wang-cangku/nexusgo:$Tag"
if ($LASTEXITCODE -ne 0) {
    Write-Host "=== Frontend image push failed ===" -ForegroundColor Red
    exit 1
}

Write-Host "`n=== Frontend image pushed successfully ===" -ForegroundColor Green
